//
//  QQSliderMenuViewController.m
//  UI进阶day01-QQ
//
//  Created by 赵玉忠 on 16/7/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "QQSliderMenuViewController.h"
#define kOffset 64
#define kMiddle (self.leftViewController.view.frame.size.width - kOffset) * 0.5
@interface QQSliderMenuViewController ()


@property (nonatomic,weak) UITapGestureRecognizer *tap;
@end

@implementation QQSliderMenuViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (instancetype)initWithLeftViewController:(UIViewController*)leftViewController andWithRootViewController:(UIViewController*)rootViewController
{
    self = [super init];
    if (self) {
        //给侧滑控制器添加视图
        [self.view addSubview:leftViewController.view];
        [self.view addSubview:rootViewController.view];

        //    //设置frame
        //    self.leftViewController.view.frame = self.view.bounds;
        //    self.rootViewController.view.frame = self.view.bounds;

        //建立父子关系
        [self addChildViewController:leftViewController];
        [self addChildViewController:rootViewController];

        //告诉系统已经添加完毕
        [leftViewController didMoveToParentViewController:self];
        [rootViewController didMoveToParentViewController:self];

        // 赋值
        self.leftViewController = leftViewController;
        self.rootViewController = rootViewController;

        [self setup];
    }

    return self;
}

- (void)setup
{
    //添加侧滑手势，这里用平移
    //创建
    UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    //添加
    [self.rootViewController.view addGestureRecognizer:pan];
}

//侧滑手势的实现啊
- (void)pan:(UIPanGestureRecognizer*)sender
{
    //获取侧滑坐标
    CGPoint p = [sender translationInView:sender.view];

    //判断松手
    switch (sender.state) {
    case UIGestureRecognizerStateBegan:
    case UIGestureRecognizerStateChanged: {
        
        //判断,初始状态不能往左滑
        if (self.rootViewController.view.frame.origin.x + p.x < 0) {
            return;
        }
        
        //偏移
        self.rootViewController.view.transform = CGAffineTransformTranslate(self.rootViewController.view.transform, p.x, 0);

        //恢复
        [sender setTranslation:CGPointZero inView:sender.view];
    }
    break;
    case UIGestureRecognizerStateEnded:
    case UIGestureRecognizerStateCancelled:
    case UIGestureRecognizerStateFailed: {
        if (self.rootViewController.view.frame.origin.x >= kMiddle) {
            [self open];
        }
        else {
            [self close];
        }
    } break;
    default:
        break;
    }
}
- (void)open
{
    self.rootViewController.view.transform = CGAffineTransformMakeTranslation(kMiddle * 2, 0);
    if (!_tap) {
        //创建
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        //添加
        [self.rootViewController.view addGestureRecognizer:tap];
        self.tap = tap;
    }
}

- (void)close
{
    [UIView animateWithDuration:2 animations:^{
        self.rootViewController.view.transform = CGAffineTransformIdentity;
    }];
    [self.rootViewController.view removeGestureRecognizer:self.tap];
    self.tap = nil;

    
}

- (void)tap:(UITapGestureRecognizer *)sender{
    [self close];
}
@end
